package com.cskaoyan.order.service.impl;

import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: jzc
 * @since: 2022/10/26 09:25
 **/
@Slf4j
@Service
public class OrderQueryServiceImpl implements OrderQueryService {


    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderConverter orderConverter;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrderShippingMapper orderShippingMapper;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        OrderListResponse orderListResponse = new OrderListResponse();
        Long total = orderMapper.countAll(request.getUserId());
        orderListResponse.setTotal(total);
        Example example = new Example(Order.class);
        //分页
        PageHelper.startPage(request.getPage(), request.getSize());
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",request.getUserId());
        example.setOrderByClause("create_time desc");

        List<Order> orders = orderMapper.selectByExample(example);



        List<OrderDetailInfo> list = new ArrayList<>();
        for (Order order : orders) {
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);
            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(order.getOrderId());
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            orderDetailInfo.setOrderItemDto(orderItemDtos);
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            orderDetailInfo.setOrderShippingDto(orderShippingDto);
            list.add(orderDetailInfo);
        }
        orderListResponse.setData(list);
        return orderListResponse;
    }

    @Override
    public OrderDetailResponse orderDetail(String orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());
        OrderDetailResponse orderDetailResponse = orderConverter.order2res(order, orderShipping);
        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
        orderDetailResponse.setGoodsList(orderItemDtos);
        return orderDetailResponse;
    }
}
